<template>
  <div class="login-wrapper">
    <div class="pulic-login-title">
      <img class="logo" src="@/assets/home/color-head.png" alt="" />
      <span class="tip">欢迎登录</span>
    </div>
    <div class="login-section">
      <div class="login-form">
        <h1 class="title">密码登录</h1>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" :hide-required-asterisk="true">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="请输入账号" clearable></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="el-icon-view" placeholder="请输入密码" clearable autocomplete="new-password" show-password></el-input>
          </el-form-item>
          <div class="form-remeber">
            <el-form-item prop="remeber">
              <el-checkbox v-model="loginForm.remeber"></el-checkbox>
              <span>15天内保持登录状态</span>
            </el-form-item>
            <router-link to="/register">忘记密码</router-link>
          </div>
        </el-form>
        <div class="success" :class="{ 'submit': isSubmit }" @click="submitForm('loginForm')">登录</div>
        <div class="last">
          没有账号，
          <router-link to="/register">
            <span class="green">免费注册</span>
          </router-link>
        </div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
      },
    };
  },
  computed: {
    isSubmit() {
      return this.loginForm.username && this.loginForm.password
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.commit("SET_TOKEN", "");
          this.storage.removeToken();
          this.$store.dispatch('login', this[formName]).then((res) => {
            if (res.code === 200) {
              this.message.success('登陆成功')
              this.$router.push({ path: '/' })
            }
          }).catch(error => {
            console.log(error)
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
};
</script>
<style scoped lang="scss">
@import "@/style/color.scss";
.login-wrapper {
  background: #f5f5f5;
}
.login-section {
  width: $bigWidth;
  height: 600px;
  padding-bottom: 60px;
  margin: 0 auto;
  position: relative;
  .login-form {
    width: 420px;
    padding: 30px 30px 60px;
    background: #fff;
    position: absolute;
    top: 90px;
    right: 0;
    color: #999999;
  }
  .title {
    font-size: 22px;
    color: #333;
    text-align: center;
    margin: 15px 0 35px;
  }
  .el-checkbox {
    margin-right: 4px;
  }
  ::v-deep .el-checkbox__inner:hover {
    border-color: $greenColor;
  }
  ::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: $greenColor;
    border-color: $greenColor;
  }
  .form-remeber {
    padding-top: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .el-form-item {
      margin-bottom: 0;
    }
  }
  .success {
    width: 100%;
    height: 50px;
    background: #EEEEEE;
    font-size: 16px;
    color: #999;
    text-align: center;
    line-height: 50px;
    cursor: pointer;
  }
  .submit {
    background: $greenColor;
    color: #fff;
  }
}
.last {
  text-align: right;
  margin-top: 15px;
  .green {
    color: $greenColor;
    cursor: pointer;
  }
}
</style>